package com.example.guet_assistant.service.tp;
import cn.hutool.http.Header;
import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONObject;
import com.example.guet_assistant.properties.AssistantProperties;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class AssistantService {

    @Resource
    private AssistantProperties properties;
    private final ObjectMapper mapper = new ObjectMapper();



    /**
     * 问答服务
     * @param resultType 结果类型，可选值选择json
     * @param messages 消息列表，其中每个元素是一个Map，包含两个键值对：type和content,指示消息类型和内容
     * @return 问答结果
     */
    public JSONObject qa(String resultType, List<Map<String, String>> messages) throws JsonProcessingException {
        Map<String,Object> body = new HashMap<>();
        body.put("resultType", resultType);
        body.put("messages", messages);
        String json = mapper.writeValueAsString(body);
        String jsonResponse = HttpRequest.post(properties.getBaseUrl())
                .header(Header.AUTHORIZATION, properties.getApiKey())
                .body(json)
                .execute().body();
        return new JSONObject(jsonResponse);
    }
}
